﻿namespace My.Hydrator.Formatters
{
    using System;
    using System.ComponentModel.DataAnnotations;

    public class ZipCodeDataTypeFormatter : BaseDataTypeFormatter, IDataTypeFormatter
    {
        private readonly string customDataType;

        private readonly bool usePlusFour;

        public ZipCodeDataTypeFormatter()
            : this("ZipCode", false)
        {
        }

        public ZipCodeDataTypeFormatter(string customDataType)
            : this(customDataType, false)
        {
        }

        public ZipCodeDataTypeFormatter(string customDataType, bool usePlusFour)
        {
            if (string.IsNullOrEmpty(customDataType))
            {
                throw new ArgumentException();
            }

            this.customDataType = customDataType;
            this.usePlusFour = usePlusFour;
        }

        public virtual DataTypeAttribute FormatterDataType
        {
            get { return new DataTypeAttribute(this.customDataType); }
        }

        public virtual object GetFormat()
        {
            var result = string.Empty;

            for (int i = 0; i < 5; i++)
            {
                result += this.Random.Next(0, 9);
            }

            if (this.usePlusFour)
            {
                var plusFour = string.Empty;

                for (int i = 0; i < 4; i++)
                {
                    plusFour += this.Random.Next(0, 9);
                }

                result = string.Format("{0}-{1}", result, plusFour);
            }

            return result;
        }
    }
}
